package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.OBUBlackListUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.OBUBlackListUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.OBUBlackListUploadDao;
import io.renren.modules.basedata.entity.Tbl_OBUBlacklist;
import io.renren.modules.basedata.service.OBUBlackListUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class OBUBlackListUploadServiceImpl implements OBUBlackListUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private OBUBlackListUploadDao obuBlackListUploadDao;

    @Override
    public String OBUBlackListUpload() throws Exception {
        log.info("开始获取未上传OBU黑名单信息");
        List<Tbl_OBUBlacklist> list = getOBUBlacklist();//获取未上传OBU黑名单信息
        Tbl_OBUBlacklist obuBlack=null;
        if(list!=null&&list.size()>0){

            for(int i=0;i<list.size();i++){
                obuBlack = list.get(i);
                OBUBlackListUploadRequest localOBUBlackListUploadRequest = new OBUBlackListUploadRequest();
                localOBUBlackListUploadRequest.setIssuerId(obuBlack.getIssuerId());
                localOBUBlackListUploadRequest.setCreationTime(DateUtil.formatDateTimes(obuBlack.getCreationTime()));
                localOBUBlackListUploadRequest.setObuId(obuBlack.getOBUId());
                localOBUBlackListUploadRequest.setType(obuBlack.getType());
                localOBUBlackListUploadRequest.setStatus(obuBlack.getStatus());
                //获取发送文件名
                String fileName = ETCCommHelper.getFileName(localOBUBlackListUploadRequest,"65");
                //生成就json文件
//				CreateFileUtil.createJsonFile(localOBUBlackListUploadRequest.toJsonStr(), "",fileName);

                try{
                    //指定响应的协议模型,调用upload发送数据
                    OBUBlackListUploadResponse response = ETCCommHelper.sdtsUpload(fileName, localOBUBlackListUploadRequest, OBUBlackListUploadResponse.class);
                    log.info("上传OBU黑名单信息成功,服务器返回："+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){
                        //上传成功,更新OBU黑名单信息表状态
                        if(localOBUBlackListUploadRequest.getStatus()==1){//加入黑名单

                            if(obuBlackListUploadDao.updateOBUBlackListUploadflag(obuBlack)>=1){//更新上传状态

                                log.info("更新OBU黑名单信息数据成功");



                            }else{

                                log.info("更新OBU黑名单信息数据失败");

                            }
                        }else{//解除黑名单

                            if(obuBlackListUploadDao.deleteUploadOBUBlack(obuBlack)==1){//删除信息
                                log.info("删除OBU黑名单信息数据成功");
                            }else{
                                log.info("删除OBU黑名单信息数据失败");
                            }
                        }


                    }else{
                        log.info("上传OBU黑名单信息失败,失败信息："+response.getInfo());

                        obuBlackListUploadDao.updateOBUBlackListUploadflagError(obuBlack);
                    }
                }catch(IOException e){
                    log.error("上传OBU黑名单信息失败,失败信息：", e);

                    obuBlackListUploadDao.updateOBUBlackListUploadflagError(obuBlack);
                    e.printStackTrace();
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传OBU黑名单信息失败,失败信息：", apie);

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        if(apie.getErrCode()==707){
                            if(obuBlack.getStatus()==1){
                                obuBlackListUploadDao.updateOBUBlackListUploadflag(obuBlack);
                            }else{
                                obuBlackListUploadDao.deleteUploadOBUBlack(obuBlack);
                            }
                        }else{
                            obuBlackListUploadDao.updateOBUBlackListUploadflagError(obuBlack);
                        }
                    }

                    apie.printStackTrace();
                    continue;
                }
            }
            return "OBU黑名单信息上传结束!";
        }else{

            return "无OBU黑名单信息,上传结束...";
        }
    }

    //获取OBU黑名单信息
    @SuppressWarnings("unchecked")
    public List<Tbl_OBUBlacklist> getOBUBlacklist() throws Exception {
        List<Tbl_OBUBlacklist> list;
        try {
            list = obuBlackListUploadDao.getOBUBlackList();
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("获取未上传OBU黑名单信息失败!错误信息：", e1);
            throw new Exception("获取未上传OBU黑名单信息失败："+e1.getMessage());
        }
        return list;
    }
}
